package com.qkl.xiatang.app.service.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.cache.LocalCacheBuilder;
import com.qkl.xiatang.app.cache.LocalSimpleCache;
import com.qkl.xiatang.app.dto.feed.FeedAllocConfigDTO;
import com.qkl.xiatang.app.entity.feed.FeedAllocConfig;
import com.qkl.xiatang.app.entity.feed.FeedAllocTemplate;
import com.qkl.xiatang.app.mapper.FeedAllocConfigMapper;
import com.qkl.xiatang.app.mapper.FeedAllocTemplateMapper;
import com.qkl.xiatang.app.utils.AppUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FeedAllocConfigService {

    @Autowired
    private FeedAllocConfigMapper feedAllocConfigMapper;

    @Autowired
    private FeedAllocTemplateMapper feedAllocTemplateMapper;


    private LocalSimpleCache<List<FeedAllocConfig>> configCache;

    private LocalSimpleCache<List<FeedAllocTemplate>> templateCache;

    @PostConstruct
    public void initCache(){
        configCache = new LocalCacheBuilder<List<FeedAllocConfig>>().build(5,null,()-> queryConfig());
        templateCache = new LocalCacheBuilder<List<FeedAllocTemplate>>().build(5,null,()-> queryTemplate());
    }

    /**
     * 查询饲料配料配置
     * @return
     */
    public List<FeedAllocConfigDTO> queryFeedAllocConfig(){
        return feedAllocConfigMapper.queryFeedAllocConfig();
    }

    /**
     *查询动保配料配置
     * @return
     */
    public List<FeedAllocConfigDTO> queryZooAllocConfig(){
        return feedAllocConfigMapper.queryZooAllocConfig();
    }

    public String formatMealNames(String templateId, Date allocDate){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy.MM.dd");
        String mealNames = fmt.format(allocDate);
        if(StringUtils.isEmpty(templateId)){
            return mealNames;
        }
        List<FeedAllocConfig> configList = this.getConfigList(templateId);
        if(ObjectUtils.isEmpty(configList)){
            return mealNames;
        }
        List<FeedAllocConfig> firstConfigList = new ArrayList<>();
        List<FeedAllocConfig> nextConfigList = new ArrayList<>();
        for (FeedAllocConfig config : configList){
            if(config.firstDate()){
                firstConfigList.add(config);
            }else {
                nextConfigList.add(config);
            }
        }
        mealNames = mealNames + getMealNames(firstConfigList);
        if(ObjectUtils.isNotEmpty(nextConfigList)){
            Date nextAllocDate = AppUtils.addDays(allocDate,1);
            SimpleDateFormat fmt2 = new SimpleDateFormat("dd日");
            mealNames = mealNames + fmt2.format(nextAllocDate) + getMealNames(nextConfigList);
        }
        return mealNames;
    }
    private String getMealNames(List<FeedAllocConfig> configList){
        StringBuffer stringBuffer = new StringBuffer();
        if(ObjectUtils.isNotEmpty(configList)){
            for (FeedAllocConfig config : configList) {
                stringBuffer.append(config.getName());
            }
        }
        return stringBuffer.toString();
    }

    public List<FeedAllocConfig> getConfigList(String configTemplateId,String companyId){
        List<FeedAllocConfig> configList = this.getConfigList(configTemplateId);
        if(ObjectUtils.isEmpty(configList)){
            List<FeedAllocTemplate>  templateList = this.getTemplateList(companyId);
            if(ObjectUtils.isNotEmpty(templateList)){
                configList = this.getConfigList(templateList.get(0).getId());
            }
        }
        return configList;
    }

    public List<FeedAllocConfig> getConfigList(String configTemplateId){
        if(StringUtils.isEmpty(configTemplateId)){
            return new ArrayList<>();
        }
        List<FeedAllocConfig> configList = this.getConfigList();
        if(ObjectUtils.isEmpty(configList)){
            return new ArrayList<>();
        }
        List<FeedAllocConfig> returnList = new ArrayList<>();
        for ( FeedAllocConfig config : configList ){
            String templateId = config.getTemplateId();
            if(StringUtils.isEmpty(templateId) || !templateId.equals(configTemplateId)){
                continue;
            }
            returnList.add(config);
        }
        return returnList;
    }

    public FeedAllocTemplate getTemplate(String companyId,String templateId){
        List<FeedAllocTemplate> templates = this.getTemplateList(companyId);
        if(ObjectUtils.isNotEmpty(templates)){
            for (FeedAllocTemplate template : templates){
                if(template.getId().equals(templateId)){
                    return template;
                }
            }
        }
        return null;
    }

    public String getTemplateName(String companyId,String templateId){
        String templateName = "";
        FeedAllocTemplate template = this.getTemplate(companyId,templateId);
        if(null != template){
            templateName = template.getName();
        }
        return templateName;
    }

    public List<FeedAllocTemplate> getTemplateList(String companyId){
        List<FeedAllocTemplate> configList = this.getTemplateList();
        if(ObjectUtils.isEmpty(configList)){
            return new ArrayList<>();
        }
        List<FeedAllocTemplate> returnList = new ArrayList<>();
        for ( FeedAllocTemplate config : configList ){
            if( config.getCompanyId().equals(companyId)){
                if(config.defaultTemplate()){
                    returnList.add(0,config);
                }else {
                    returnList.add(config);
                }
            }
        }
        return returnList;
    }

    public List<FeedAllocConfig> getConfigList(){
       return configCache.getCache();
    }

    public List<FeedAllocTemplate> getTemplateList(){
        return templateCache.getCache();
    }


    private List<FeedAllocTemplate> queryTemplate(){
        List<FeedAllocTemplate>  list =  feedAllocTemplateMapper.selectList(new QueryWrapper<FeedAllocTemplate>()
                                                                                .isNotNull("company_id")
                                                                                .ne("company_id",""));
        if(ObjectUtils.isEmpty(list)){
            list = new ArrayList<>();
        }
        return list;
    }

    private List<FeedAllocConfig> queryConfig(){
        List<FeedAllocConfig>  list =  feedAllocConfigMapper.selectList(new QueryWrapper<FeedAllocConfig>()
                                                                            .isNotNull("company_id")
                                                                            .ne("company_id","")
                                                                            .orderByAsc("start_time"));
        if(ObjectUtils.isEmpty(list)){
            list = new ArrayList<>();
        }
        return list;
    }

}
